package com.example.backupgeo.service.impl;

import com.example.backupgeo.common.RoleEnum;
import com.example.backupgeo.entity.Teacher;
import com.example.backupgeo.mapper.TeacherMapper;
import com.example.backupgeo.service.TeacherService;
import com.example.backupgeo.util.ResStatus;
import com.example.backupgeo.util.ResultVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public ResultVO findAll() {
        List<Teacher> teachers = teacherMapper.findAll();
        if(teachers.isEmpty()){
            return new ResultVO(ResStatus.OK, ResStatus.noData,null);
        } else {
            return new ResultVO(ResStatus.OK, ResStatus.SUCCESS,teachers);
        }
    }

    @Override
    public ResultVO queryTeacherInfoById(String id) {
        if (id == null) {
            return new ResultVO(ResStatus.OK, ResStatus.messageCannotEmpty,null);
        } else {
            Teacher teacher = teacherMapper.queryTeacherInfoById(id);
            return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,teacher);
        }
    }

    @Override
    public ResultVO addTeacher(Teacher teacher) {
        if (teacher == null) {// 不能为空
            return new ResultVO(ResStatus.NO, ResStatus.isNotExist,null);
        } else {
            Teacher teacherVO = teacherMapper.queryTeacherByName(teacher.getUsername());
            // 不存在才需要添加
            if (teacherVO != null) {
                return new ResultVO(ResStatus.NO,ResStatus.isExisted,null);
            } else {
                try {
                    teacher.setRole(RoleEnum.TEACHER.name());
                    int res = teacherMapper.insertTeacher(teacher);
                    if (res > 0) {
                        return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,null);
                    } else {
                        return new ResultVO(ResStatus.NO,ResStatus.operationFailure,null);
                    }

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }

    @Override
    public ResultVO update(Teacher teacher) {
        try {
            int res = teacherMapper.update(teacher);
            if (res > 0) {
                return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,null);
            } else {
                return new ResultVO(ResStatus.NO,ResStatus.operationFailure,null);
            }
        } catch (Exception e) {
            return new ResultVO(ResStatus.NO,e.getMessage(),null);
        }

    }

    @Override
    public ResultVO deleteTeacher(Integer id) {
        try {
            int res = teacherMapper.deleteTeacher(id);
            if (res > 0) {
                return new ResultVO(ResStatus.OK,ResStatus.SUCCESS,null);
            } else {
                return new ResultVO(ResStatus.NO,ResStatus.operationFailure,null);
            }
        } catch (Exception e) {
            return new ResultVO(ResStatus.NO,e.getMessage(),null);
        }
    }

}
